package com.bosch.in.rtp1.ept.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.bosch.in.rtp1.ept.constant.CommonConstants;
import com.bosch.in.rtp1.ept.constant.FactorType;
import com.bosch.in.rtp1.ept.constant.FinishRecipeConstant;
import com.bosch.in.rtp1.ept.constant.ReleasedHoldParameter;
import com.bosch.in.rtp1.ept.model.dto.FshRcpChartFactors;
import com.bosch.in.rtp1.ept.model.dto.LineChartPointFactor;
import com.bosch.in.rtp1.ept.model.dto.RelHldChartFactor;

public class ChartUtility {
	
	/**
	 * Convert List of ChartFactors to LineChartPointFactor
	 * @param  List<ChartFactors> exFactors, int year
	 * @return List<FinishedRecipesFactor>
	 */
	public static List<LineChartPointFactor> fromFshRcpChartFactorsToFshRcpFactor(List<FshRcpChartFactors> exFactors, int year)
	{
		//initialize 
		List<LineChartPointFactor> xPlanList 	= new ArrayList<LineChartPointFactor>();
		List<LineChartPointFactor> xRWList 		= new ArrayList<LineChartPointFactor>();
		List<LineChartPointFactor> xRWHList 	= new ArrayList<LineChartPointFactor>();
		List<LineChartPointFactor> fxPlanList 	= new ArrayList<LineChartPointFactor>();
		List<LineChartPointFactor> fxRWList 	= new ArrayList<LineChartPointFactor>();
		List<LineChartPointFactor> fxRWHList 	= new ArrayList<LineChartPointFactor>();
		List<LineChartPointFactor> factors 		= new ArrayList<LineChartPointFactor>();
		
		int index = 0;
		int i=0;
		while (i < FinishRecipeConstant.NUM_OF_TIME_POINT && index < exFactors.size())
		{
			FshRcpChartFactors exFactor = exFactors.get(index);
			
			//month value will be previous year if current record has month equal to 0
			String month = i == 0 ? String.valueOf(year - 1 ) : CommonConstants.MONTH_LIST[i] ;
			String comment = FinishRecipeConstant.COUNT_RECIPE_STRING  + String.valueOf(exFactor.getCount());
			if (exFactor.getMonth() == i)
			{
				xPlanList.add	(new LineChartPointFactor(exFactor.getxPlan(),	month,FactorType.X_PLAN	,comment));
				xRWList.add		(new LineChartPointFactor(exFactor.getxRW(),	month,FactorType.X_RW	,comment));
				xRWHList.add	(new LineChartPointFactor(exFactor.getxRWH(),	month,FactorType.X_RWH	,comment));
				fxPlanList.add	(new LineChartPointFactor(exFactor.getFxPlan(),month,FactorType.FX_PLAN,""));
				fxRWList.add	(new LineChartPointFactor(exFactor.getFxRW(),	month,FactorType.FX_RW	,""));
				fxRWHList.add	(new LineChartPointFactor(exFactor.getFxRWH(),	month,FactorType.FX_RWH	,""));
				++index;
			}
			else
			{
				xPlanList.add	(new LineChartPointFactor(null,month,FactorType.X_PLAN	,comment));
				xRWList.add		(new LineChartPointFactor(null,month,FactorType.X_RW	,comment));
				xRWHList.add	(new LineChartPointFactor(null,month,FactorType.X_RWH	,comment));
				fxPlanList.add	(new LineChartPointFactor(null,month,FactorType.FX_PLAN,""));
				fxRWList.add	(new LineChartPointFactor(null,month,FactorType.FX_RW	,""));
				fxRWHList.add	(new LineChartPointFactor(null,month,FactorType.FX_RWH	,""));
			}
			++i;
		}
		while (i<FinishRecipeConstant.NUM_OF_TIME_POINT)
		{
			String month = i == 0 ? String.valueOf(year - 1) : CommonConstants.MONTH_LIST[i] ;
			xPlanList.add	(new LineChartPointFactor(null,month,FactorType.X_PLAN,""));
			xRWList.add		(new LineChartPointFactor(null,month,FactorType.X_RW,""));
			xRWHList.add	(new LineChartPointFactor(null,month,FactorType.X_RWH,""));
			fxPlanList.add	(new LineChartPointFactor(null,month,FactorType.FX_PLAN,""));
			fxRWList.add	(new LineChartPointFactor(null,month,FactorType.FX_RW,""));
			fxRWHList.add	(new LineChartPointFactor(null,month,FactorType.FX_RWH,""));
			++i;
		}
		
		updateAllXLines(xPlanList,xRWList,xRWHList,fxPlanList,fxRWHList,fxRWList,year);
		
		factors.addAll(xPlanList);
		factors.addAll(xRWList);
		factors.addAll(xRWHList);
		
		factors.addAll(fxPlanList);
		factors.addAll(fxRWList);
		factors.addAll(fxRWHList);
		
		return factors;
	}
	
	/**
	 * Convert List of RelHldChartFactor to LineChartPointFactor
	 * @param  List<ChartFactors> exFactors, int year
	 * @return List<FinishedRecipesFactor>
	 */
	public static List<LineChartPointFactor> fromRelHldChartFactorsToFshRcpFactor(List<RelHldChartFactor> relHldChartFactors, int year)
	{
		//initialize 
		List<LineChartPointFactor> prio1 	= new ArrayList<LineChartPointFactor>();
		List<LineChartPointFactor> prio2 	= new ArrayList<LineChartPointFactor>();
		List<LineChartPointFactor> prio3 	= new ArrayList<LineChartPointFactor>();		
		List<LineChartPointFactor> factors 	= new ArrayList<LineChartPointFactor>();
		
		int index = 0;
		int i=0;
		while (i < FinishRecipeConstant.NUM_OF_TIME_POINT && index < relHldChartFactors.size())
		{
			RelHldChartFactor exFactor = relHldChartFactors.get(index);
			
			//month value will be previous year if current record has month equal to 0
			String month = i == 0 ? String.valueOf(year - 1 ) : CommonConstants.MONTH_LIST[i] ;
			
			if (exFactor.getMonth() == i)
			{
				prio1.add	(new LineChartPointFactor(exFactor.getPrio1(),	month,FactorType.PRIO_1	,ReleasedHoldParameter.HOLD_COUNT_STRING + exFactor.getCountPrio1()));
				prio2.add	(new LineChartPointFactor(exFactor.getPrio2(),	month,FactorType.PRIO_2	,ReleasedHoldParameter.HOLD_COUNT_STRING + exFactor.getCountPrio2()));
				prio3.add	(new LineChartPointFactor(exFactor.getPrio3(),	month,FactorType.PRIO_3	,ReleasedHoldParameter.HOLD_COUNT_STRING + exFactor.getCountPrio3()));
				
				++index;
			}
			else
			{
				prio1.add	(new LineChartPointFactor(null,month,FactorType.PRIO_1	,ReleasedHoldParameter.HOLD_COUNT_STRING + exFactor.getCountPrio1()));
				prio2.add	(new LineChartPointFactor(null,month,FactorType.PRIO_2	,ReleasedHoldParameter.HOLD_COUNT_STRING + exFactor.getCountPrio2()));
				prio3.add	(new LineChartPointFactor(null,month,FactorType.PRIO_3	,ReleasedHoldParameter.HOLD_COUNT_STRING + exFactor.getCountPrio3()));
			}
			++i;
		}
		while (i<FinishRecipeConstant.NUM_OF_TIME_POINT)
		{
			String month = i == 0 ? String.valueOf(year - 1) : CommonConstants.MONTH_LIST[i] ;
			prio1.add	(new LineChartPointFactor(null,month,FactorType.PRIO_1	,ReleasedHoldParameter.HOLD_COUNT_STRING + 0));
			prio2.add	(new LineChartPointFactor(null,month,FactorType.PRIO_2	,ReleasedHoldParameter.HOLD_COUNT_STRING + 0));
			prio3.add	(new LineChartPointFactor(null,month,FactorType.PRIO_3	,ReleasedHoldParameter.HOLD_COUNT_STRING + 0));
			++i;
		}
		
		updateXLinesValue(prio1, year);
		updateXLinesValue(prio2, year);
		updateXLinesValue(prio3, year);
		
		factors.addAll(prio1);
		factors.addAll(prio2);
		factors.addAll(prio3);
		
		return factors;
	}
	
	/**
	 * Get the string which represents the month and year in the parameters 
	 * @param year
	 * @param month
	 * @return String
	 */
	public static String getMonthDisplay(int year, int month)
	{
		int currentYear  = Calendar.getInstance().get(Calendar.YEAR);
		int currentMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
		int currentDay	 = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
		String returnValue = String.valueOf(year);
		if (month>0)
		{
			if (month==currentMonth && year==currentYear)
			{
				returnValue = CommonConstants.MONTH_LIST[month]+' ' + currentDay ;
			}
			else
			{
				returnValue = CommonConstants.MONTH_LIST[month];
			}
			
			if (month == 1)
			{
				returnValue = returnValue + " " + year;
			}
		}
		
		return returnValue;
	}
	/***********PRIVATE METHOD****************/
	
	/**
	 * Update the display string of each month 
	 */
	private static void updateAllXLines(List<LineChartPointFactor> xPlanList,
			List<LineChartPointFactor> xRWList,
			List<LineChartPointFactor> xRWHList,
			List<LineChartPointFactor> fxPlanList,
			List<LineChartPointFactor> fxRWHList,
			List<LineChartPointFactor> fxRWList,int year) {
		
		updateXLinesValue(xPlanList,year);
		updateXLinesValue(xRWList,year);
		updateXLinesValue(xRWHList,year);
		
		updateXLinesValue(fxPlanList,year);
		updateXLinesValue(fxRWList,year);
		updateXLinesValue(fxRWHList,year);
	}

	/**
	 * Update the display string of each month 
	 * @param factors
	 * @param year
	 */
	private static void updateXLinesValue(List<LineChartPointFactor> factors,int year)
	{
		int currentYear  = Calendar.getInstance().get(Calendar.YEAR);
		int currentMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
		int currentDay	 = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
		
		//add day if we're currently in this year
		
		if (year == currentYear)
		{
			String currentMonthString = factors.get(currentMonth).getMonth();
			factors.get(currentMonth).setMonth(currentMonthString + " " + currentDay);
		}
		
		//add year into first month  
		String firstMonth = factors.get(1).getMonth();
		factors.get(1).setMonth(firstMonth +" " + year);
	}
	
}
